package com.padakeji.android.pworks.example.retrofit;

import android.os.Bundle;
import android.util.Log;

import com.padakeji.android.pworks.app.PadaActivity;
import com.padakeji.android.pworks.example.R;
import com.padakeji.android.pworks.example.retrofit.api.TestApi;
import com.padakeji.android.pworks.example.retrofit.bean.GithubUser;
import com.padakeji.android.pworks.ui.UIHelper;

import java.io.IOException;

import retrofit.Call;
import retrofit.Callback;
import retrofit.GsonConverterFactory;
import retrofit.Response;
import retrofit.Retrofit;

/**
 * Created by AtanL on 2015/9/22.
 */
public class RetrofitTestActivity extends PadaActivity {

    private static final String TAG = "RetrofitTestActivity";

    private UIHelper uiHelper;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_retrofit_test);

        uiHelper = new UIHelper(this);

        Retrofit retrofit = new Retrofit.Builder()
                .baseUrl("https://api.github.com")
                .addConverterFactory(GsonConverterFactory.create())
                .build();

        TestApi service = retrofit.create(TestApi.class);
        Call<GithubUser> c = service.getGithubUser("atanl");
        c.enqueue(new Callback<GithubUser>() {
            @Override
            public void onResponse(Response<GithubUser> response, Retrofit retrofit) {
                uiHelper.showToast(response.body().login);
            }

            @Override
            public void onFailure(Throwable t) {
                Log.e(TAG, t.getMessage());
            }
        });

    }
}
